*** This code makes a table about who might benefit from pre-population

cap log close
clear
set more off
set type double
set linesize 150

log using "$logdir/analysis/table_who_benefits.log" , replace


local thresh 100


****************
* Prepare data *
****************

use "$statadir/soi_cdw_taxsim_2019.dta" if nonfiler==0 , clear

count

quietly do "$adir/get_bad_dummies"
get_bad_dummies
*NOTE: this calculates the item-based failure indicators at the default tolerance of $100

gen byte good_any = 1 - bad_any

drop bad_any

egen double bad_total = rowtotal(bad_*)

gen double age_prim = 2019 - soi_prim_yob if !missing(soi_prim_yob) & soi_prim_yob!=0

foreach xx of numlist 1 4 {
	gen double delta_tax`xx'     = taxsim`xx'_fed_incm_tax - soi_fed_incm_tax
	gen double delta_tax`xx'_pos = delta_tax`xx' if delta_tax`xx'>0
	gen double delta_tax`xx'_neg = delta_tax`xx' if delta_tax`xx'<0
	
	gen byte d_correct`thresh'_`xx' = (abs(delta_tax`xx')<`thresh')
	gen byte d_over`thresh'_`xx'    = (delta_tax`xx'>`thresh')
	gen byte d_under`thresh'_`xx'   = (delta_tax`xx'<-`thresh')
}

gen byte d_married  = inrange(soi_fil_stat,2,3)
gen byte d_deps     = (soi_deps_tot>0)
gen byte d_paidprep = (soi_prep==1)



************************
* Table: who benefits? *
************************

local vlist1 "d_married d_deps d_paidprep"
local vlist2 "age_prim soi_agi taxsim1_fed_incm_tax soi_fed_incm_tax delta_tax1 bad_total"

sum `vlist1' [aw=soi_wgt] if good_any==1
sum `vlist2' [aw=soi_wgt] if good_any==1 , d

sum `vlist1' [aw=soi_wgt] if good_any==0
sum `vlist2' [aw=soi_wgt] if good_any==0 , d

sum `vlist1' [aw=soi_wgt] if d_correct`thresh'_1==1
sum `vlist2' [aw=soi_wgt] if d_correct`thresh'_1==1 , d

sum `vlist1' [aw=soi_wgt] if d_over`thresh'_1==1
sum `vlist2' [aw=soi_wgt] if d_over`thresh'_1==1 , d

sum `vlist1' [aw=soi_wgt] if d_under`thresh'_1==1
sum `vlist2' [aw=soi_wgt] if d_under`thresh'_1==1 , d


cap log close
